package application;

import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.io.File;

import javax.swing.AbstractAction;
import javax.swing.SwingUtilities;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;

import application.script.XMLProgramBuilder;
import application.visual.MainFrame;

public class Launcher {

	private static MainFrame frame;
	
	public static void main(String[] args) throws Exception
	{
		//XMLProgramBuilder build = new XMLProgramBuilder(new File(root + "/lib/skeleton.xml"));
		//System.out.println(build.getProgramString());
		SwingUtilities.invokeLater(new Runnable(){
			public void run(){
				getMainFrame().setMenuBar(getMenuBar());
				getMainFrame().setVisible(true);	
			}
		});
				
		/*final IOClient c = SerialCommunicator.getCommunicator("COM2");
		c.addListener(new CommunicationListener(){
			
			public void communicationRequested(SerialPortEvent event) {
				
				System.out.println(new String(c.receive()));
			}
		});
		while(true)
		{
			String text = JOptionPane.showInputDialog("Message");
			c.send(text.getBytes());
		}*/
	}
	
	public static MainFrame getMainFrame()
	{
		
		if(frame == null)
		{
			frame = new MainFrame();
		}
		
		return frame;
	}
	
	
	private static MenuBar getMenuBar()
	{
		MenuBar bar = new MenuBar();
		
		Document d = XMLUtil.getDocument(new File(root + "/lib/menu.xml"));
		Element rootEl = d.getDocumentElement();
		
		buildMenu(rootEl, null, bar);
		
		return bar;
		
	}
	
	private static void buildMenu(Node n, Menu parent, MenuBar bar)
	{
		for(int i = 0 ; i < n.getChildNodes().getLength(); i++)
		{
			Node n2 = n.getChildNodes().item(i);
			if(!(n2 instanceof Element))
				continue;
			
			if(n2.getNodeName().equals("separator"))
			{
				if(parent != null)
					parent.addSeparator();
				
				buildMenu(n2, parent, bar);
			}
			else if(n2.getNodeName().equals("menu"))
			{
				NamedNodeMap atts = n2.getAttributes();
				
				Node textNode = atts.getNamedItem("text");
				if(textNode == null)
					continue;
				
				String text = textNode.getTextContent();
				
				Menu m = new Menu(text);
				
				if(parent != null)
					parent.add(m);
				else
					bar.add(m);
				
				buildMenu(n2, m, bar);
					
			}
			else if(n2.getNodeName().equals("menu-item"))
			{
				NamedNodeMap atts = n2.getAttributes();
				
				Node textNode = atts.getNamedItem("text");
				Node actionNode = atts.getNamedItem("action");	
				
				if(textNode == null)
					continue;
					
				String text = textNode.getTextContent();
				MenuItem item = new MenuItem();
				
				item.setLabel(text);
				
				if(actionNode == null)
					item.setEnabled(false);
				
				String action = actionNode.getTextContent();
				
				try{
					Class<AbstractAction> clazz = (Class<AbstractAction>)Class.forName(action);
					item.addActionListener(clazz.newInstance());
				}catch(Exception e)
				{
					item.setEnabled(false);
				}
				
				if(parent != null)
					parent.add(item);
				
				buildMenu(n2, null, bar);
			}
		}
	}
	
	
	public static String root = getRoot();
	private static String getRoot()
	{
		String root = "/";
		try{
				root = FileUtil.getCodebaseDirectory(Launcher.class).getParent();
		}catch(Exception e){e.printStackTrace();}
		
		return root;
	}
}
